﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace AutoGuiHelp
{
    public partial class FrmMain : Form
    {
        int desc = 160;

        public FrmMain ()
        {
            InitializeComponent ();
        }

        private void FrmMain_KeyDown (object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.F1)
            {
                List<Bitmap> bitmaps = new List<Bitmap>();

                Bitmap bSrc = GuiHelper.getControlImage(this);

                foreach (Control control in Controls)
                {
                    Bitmap bDst = new Bitmap(bSrc);
                    Point   xxx     = new Point (0, 0);
                    Point   ppp     = control.PointToScreen (xxx);
                    int     trueleft= ppp.X - Left;
                    int     truetop = ppp.Y - Top;

                    for (int w = 0; w < bSrc.Width; w++)
                    {
                        for (int h = 0; h < bSrc.Height; h++)
                        {
                            Color c = bSrc.GetPixel (w, h);
                            Color c2;
                            
                            if ((w >= trueleft  && w <= (control.Width + trueleft ))
                                &&
                               (h >= truetop  && h <= (control.Height + truetop )))
                            {
                                c2 = c;
                            }
                            else
                            {
                                c2 = Color.FromArgb (
                                    (c.R > desc) ? (c.R - desc) : 0,
                                    (c.G > desc) ? (c.G - desc) : 0,
                                    (c.B > desc) ? (c.B - desc) : 0
                                );
                            }

                            bDst.SetPixel(w, h, c2);
                        }
                    }

                    Graphics g = Graphics.FromImage(bDst);
                    g.DrawRectangle (new Pen (new SolidBrush (Color.Lime), 2),
                        new Rectangle (
                            new Point   (trueleft, truetop),
                            new Size    (control.Width, control.Height)
                        )
                    );
                    g.Dispose ();
                    
                    bitmaps.Add (bDst);
                }

                FrmAutoGuiHelp f = new FrmAutoGuiHelp (bitmaps.ToArray());
                f.ShowDialog();
                f.Dispose   ();
            }
        }
    }
}
